(function ($) {
    $.fn.yqTextNCheckBox = function (opts, param) {
        if (typeof opts == 'string') {
            return $.fn.yqTextNCheckBox.methods[opts](this, param);
        }

        return this.each(function () {
            $this = $(this);
            var opt = $.extend({}, $.fn.yqTextNCheckBox.defaults, opts);

            var $uniqueID = $this.attr("ID");
            if ($uniqueID === undefined) {
                var id = 'yyxxxxxxxx'.replace(/[xy]/g, function (c) {
                    var r = Math.random() * 16 | 0,
                        v = c == 'x' ? r : (r & 0x3 | 0x8);
                    return v.toString(16);
                });

                $this.attr("ID", id);
            }

            if (opt.width === undefined && $this.width() > 0) {
                opt.width = $this.width();
            }
            else if (typeof opt.width != "undefined") {
                $this.width(opt.width);
            }

            opt.onload = true;

            //变量信息
            $this.data("textncheckbox_opt", opt);
            $this.data("yqtype", "yqTextNCheckBox");

            init_text($this);
        });
    };

    $.fn.yqTextNCheckBox.defaults = {
        lists: [],
        value: null,
        blank: true,
        isEdit: true,   //可编辑
        editModeW: 400, //模态编辑器宽度 格式为 800（数字）
        editModeH: 300, //模态编辑器高度 格式为 600（数字）
        afterChange: function (value) {

        }
    };

    $.fn.yqTextNCheckBox.methods = {
        options: function (jq) {
            return options($(jq[0]));
        },
        destroy: function (jq) {
            destroy($(jq[0]));
        },
        resize: function (jq, width) {
            resize($(jq[0]), width);
        },
        clear: function (jq, data) {
            setValue($(jq[0]), null);
        },
        setValue: function (jq, value) {
            setValue($(jq[0]), value);
        },
        getValue: function (jq) {
            return getValue($(jq[0]));
        },
        isEdit: function (jq, tf) {
            isEdit($(jq[0]), tf);
        },
    };

    function options($object) {
        return $object.data("textncheckbox_opt");
    }

    function destroy($object) {
        $object.textbox("destroy");
    }

    function resize($object, width) {
        $object.textbox("resize", width);
    }

    function init_text($object) {
        var opt = options($object);
        var $uniqueID = $object.attr('id');

        var text_opt = {
            editable: false,
            iconAlign: 'right',
            icons: [{
                iconCls: 'yqwin-combo-btn-icon fa fa-pencil',
                handler: function (e) {
                    //console.log("e===",e);
                    var pp = $object.textbox("textbox");
                    prevent_e = $.getPreventDefault(pp[0]);
                    prevent_e.id = $object.attr("id");
                    prevent_e.options = opt;

                    var mdiinfo = $.yqGetWindParams();

                    var run_opt = {
                        mdiinfo: mdiinfo,
                        prevent_e: prevent_e,
                        width: opt.editModeW,
                        height: opt.editModeH,

                        lists: opt.lists,
                        default: opt.default,
                        value: opt.value,
                        isEdit: opt.isEdit,

                        callback: function (val) {
                            //嵌入到datagrid中，兼容处理（需要重新创建对象）
                            if (typeof opt.p_target != "undefined") {
                                var ed = $.yqTextEditor(opt.p_target, opt.p_field);
                                if (!(ed === null)) {
                                    if (opt.value != val) {
                                        opt.afterChange.call($object, val);
                                    }
                                    $(ed.target).yqTextNCheckBox('setValue', val);
                                }
                            }
                            else {
                                if (opt.value != val) {
                                    opt.afterChange.call($object, val);
                                }
                                setValue($object, val);
                            }
                        }
                    };

                    if (window.top == window.self) {
                        $.yqNCheckboxPopEdit(run_opt);
                    }
                    else {
                        window.top.$.yqNCheckboxPopEdit(run_opt);
                    }
                }
            }, {
                iconCls: 'yqwin-combo-btn-icon fa fa-times',
                //disabled:true,
                handler: function (e) {
                    setValue($object, null);
                }
            }]
        };

        $object.textbox(text_opt);
        setValue($object, opt.value);
        isEdit($object, opt.isEdit);
    }

    function setValue($object, value) {
        var opt = options($object);
        if (value === undefined || value == "") {
            value = null;
        }
        opt.value = value;

        var b_value = "";
        if (!(value === null)) {
            for (let i = 0; i < opt.lists.length; i++) {
                if (("," + value + ",").indexOf("," + opt.lists[i]["no"] + ",") != -1) {
                    b_value += (b_value == "" ? "" : ",") + opt.lists[i]["name"];
                }
            }
        }
        $object.textbox("setValue", b_value);
    }

    function getValue($object) {
        var opt = options($object);
        return opt.value;
    }

    function isEdit($object, tf) {
        var opt = options($object);
        var icon0 = $object.textbox("getIcon", 0);
        var icon1 = $object.textbox("getIcon", 1);
        opt.isEdit = tf;
        if (opt.isEdit) {
            icon0.removeClass('icon-more');
            icon0.addClass('icon-edit');
            icon1.show();
            //icon1.removeClass('textbox-icon-disabled');
        } else {
            icon0.removeClass('icon-edit');
            icon0.addClass('icon-more');
            //icon1.addClass('textbox-icon-disabled');
            icon1.hide();
        }
    }
})(jQuery);